// Copyright (C) 2022 即时通讯网(52im.net) & Jack Jiang.
// The RainbowChat Project. All rights reserved.
//
// 【本产品为著作权产品，合法授权后请放心使用，禁止外传！】
// 【本次授权给：<广西木子科技有限公司>，授权编号：<NT220402151538>，代码指纹：<A.648883738.885>，技术对接人微信：<ID: Lingmuziyi>】
// 【授权寄送：<收件：李先生、地址：南宁市科园西十路11号国电智能大厦1101F、电话：17736659550、邮箱：yingshashou@vip.qq.com>】
//
// 【本系列产品在国家版权局的著作权登记信息如下】：
// 1）国家版权局登记名(简称)和权证号：RainbowChat    （证书号：软著登字第1220494号、登记号：2016SR041877）
// 2）国家版权局登记名(简称)和权证号：RainbowChat-Web（证书号：软著登字第3743440号、登记号：2019SR0322683）
// 3）国家版权局登记名(简称)和权证号：RainbowAV      （证书号：软著登字第2262004号、登记号：2017SR676720）
// 4）国家版权局登记名(简称)和权证号：MobileIMSDK-Web（证书号：软著登字第2262073号、登记号：2017SR676789）
// 5）国家版权局登记名(简称)和权证号：MobileIMSDK    （证书号：软著登字第1220581号、登记号：2016SR041964）
// * 著作权所有人：江顺/苏州网际时代信息科技有限公司
//
// 【违法或违规使用投诉和举报方式】：
// 联系邮件：jack.jiang@52im.net
// 联系微信：hellojackjiang
// 联系QQ号：413980957
// 授权说明：http://www.52im.net/thread-1115-1-1.html
// 官方社区：http://www.52im.net
package com.x52im.mall.shop;

import java.util.ArrayList;
import java.util.Vector;

import com.eva.framework.Processor;
import com.eva.framework.dbpool.DBShell;
import com.eva.framework.dto.SysActionConst;
import com.eva.framework.utils.EndsExceptionFactory;
import com.x52im.mall.shop.dto.Color;
import com.x52im.mall.shop.dto.Comment;
import com.x52im.mall.shop.dto.Device;
import com.x52im.mall.shop.dto.Feature;
import com.x52im.mall.shop.dto.Pic;

/**
 * 购物管理功能集业务处理器.
 *
 * @author Jack Jiang(http://www.52im.net/space-uid-1.html)
 * @version 1.0
 */
public class ShopProcessor$NoNeedAuthed implements SysActionConst, Processor {
    private static DBShell db = new DBShell();

    /**
     * 处理器的核心实现方法.<br>
     * 可据job_dispatch_id找到对应的JobDispatcher，JobDispatcher又能据
     * action_id进行对应动作的处理，以完成本次操作请求.
     *
     * @param job_dispatch_id 作业调度ID
     * @param action_id       动作ID
     * @param Object          newData 客端发送过来的本次修改新数据(可能为空，理论上与oldData不会同时空）
     * @param Object          oldData 客端发送过来的本次修改前的老数据(可能为空，理论上与newData不会同时空）
     * @param UserRoot        user 包含该请求用户完整个人信息封装对象（该对象来自于成功登陆后存放于session中的值）
     * @return 任意类型返回值
     * @throws Exception
     */
    @Override
    public Object process(int job_dispatch_id, int action_id, Object newData, Object oldData, User user) throws Exception {
        Object ret = null;
        switch (job_dispatch_id) {
            case com.x52im.mall.shop.dto.JobDispatchConst.SHOP_GOODS_MGR_NO$NEED$AUTHED:
                ret = goodsMgrJobDispatcher(action_id, newData, oldData, user);
                break;
            default:
                throw EndsExceptionFactory.INVALID_JOB_DISPATCHER_ID(job_dispatch_id);
        }
        return ret;
    }

    /**
     * 维护商品管理相关操作的作业调度.
     *
     * @param action_id 维护类型，SysConstant中的操作类型常量
     * @param newData   客端发送过来的本次修改新数据(可能为空，理论上与oldData不会同时空）
     * @param oldData   客端发送过来的本次修改前的老数据(可能为空，理论上与newData不会同时空）
     * @param userName  操作者的用户名
     * @return 返回给客户端的对象
     */
    @SuppressWarnings("unchecked")
    public Object goodsMgrJobDispatcher(int action_id, Object newData, Object oldData, User u) throws Exception {
        Vector sqls = new Vector(2);
        Vector values = new Vector(2);
        switch (action_id) {
//			case ACTION_NEW:
//			{
//				SO so = (SO) newData;
//				String orderId = ONHelper.getON("sales_so_order_id");
//				so.setOrder_id(orderId);
//				Date date = new Date();
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//				sqls.add("INSERT INTO sales_so(order_id,order_type,for_user_uid," +
//						"order_date,order_status,pay_type,order_currency," +
//						"order_amount,transfer_id,fare_amount,order_total,pay_time,appender)" +
//						"VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?)");
//				values.add(new Object[] {
//							orderId,so.getOrder_type(),so.getFor_user_uid(),sdf.format(date),
//							so.getOrder_status(),so.getPay_type(),so.getOrder_currency(),
//							so.getOrder_amount(),so.getTransfer_id(),so.getFare_amount(),
//							so.getOrder_total(),so.getPay_time(),so.getAppender()
//				});
//				List<SODetail> details = so.getSoDetails();
//				for(SODetail d : details){
//					sqls.add("INSERT INTO sales_so_detail(so_detail_id,order_id," +
//							"device_id,color_id,purchase_quantity,purchase_price," +
//							"purchase_amount,appender)" +
//							"VALUES(?,?,?,?,?,?,?,?)");
//					values.add(new Object[] {
//							SNHelper.getSN("sales_so_detail_id"),orderId,d.getDevice_id(),
//							d.getColor_id(),d.getPurchase_quantity(),d.getPurchase_price(),
//							d.getPurchase_amount(),d.getAppender()
//					});
//				}
//				SOConsigneeInfo ci = so.getSoConsigneeInfo();
//				sqls.add("INSERT INTO sales_so_consignee_info(order_id,consignee_surname," +
//						"consignee_name,country_of_consignee,city_of_consignee,addr_of_consignee," +
//						"code_of_consignee,mail_of_consignee,phone_of_consignee,appender) " +
//						"VALUES(?,?,?,?,?,?,?,?,?,?)");
//				values.add(new Object[] {
//						orderId,ci.getConsignee_surname(),ci.getConsignee_name(),
//						ci.getCode_of_consignee(),ci.getCity_of_consignee(),ci.getAddr_of_consignee(),
//						ci.getCode_of_consignee(),ci.getMail_of_consignee(),ci.getPhone_of_consignee(),
//						ci.getAppender()
//				});
//
//				boolean res = db.update(sqls, values, true);
//				if(res)
//					return so;
//				return null;
//			}
            // 查询商品列表
            case ACTION_QUERY: {
                String sql =
                        "select " +
                                "device_id" +
                                ",device_name" +
                                ",device_short_name" +
                                ",for_sex" +
                                ",reference_price" +
                                ",sale_price" +
                                ",sale_currency_type" +
                                ",device_basic_info" +
                                ",device_sale_info" +
                                ",device_detail_url" +
                                ",device_desc" +
                                " from base_devices where is_valid=1";

                Vector<Vector> vec = db.queryData(sql);
                ArrayList<Device> datas = new ArrayList<Device>();
                if (vec.size() > 0) {
                    for (Vector row : vec) {
                        int j = 0;
                        Device d = new Device();
                        d.setDevice_id((String) row.get(j++));
                        d.setDevice_name((String) row.get(j++));
                        d.setDevice_short_name((String) row.get(j++));
                        d.setFor_sex((String) row.get(j++));
                        d.setReference_price((String) row.get(j++));
                        d.setSale_price((String) row.get(j++));
                        d.setSale_currency_type((String) row.get(j++));
                        d.setDevice_basic_info((String) row.get(j++));
                        d.setDevice_sale_info((String) row.get(j++));
                        d.setDevice_detail_url((String) row.get(j++));
                        d.setDevice_desc((String) row.get(j++));

                        d.setFeature(getFeature(d.getDevice_id()));

                        datas.add(d);
                    }
                }

                return datas;
            }

            // 查询商品对应的颜色明细数据
            case ACTION_APPEND1: {
                ArrayList<Color> colorsForRet = new ArrayList<Color>();
                String device_id = (String) newData;

                // 查询对应设备的颜色
                String sql = "select color_id,color_desc from device_colors where device_id='" + device_id + "'";
                Vector<Vector> colors = db.queryData(sql);
                for (Vector row : colors) {
                    int j = 0;
                    String color_id = (String) row.get(j++);
                    String color_desc = (String) row.get(j++);

                    // 颜色DTO
                    Color c = new Color();
                    c.setDevice_id(device_id);
                    c.setColor_id(color_id);
                    c.setColor_desc(color_desc);

                    // ** 取出对应颜色的图片明细
                    String sql2 = "select pic_id,pic_desc,pic_path from device_pics where color_id='" + color_id + "'";
                    Vector<Vector> picsForColor = db.queryData(sql2);
                    for (Vector picRow : picsForColor) {
                        int k = 0;
                        String pic_id = (String) picRow.get(k++);
                        String pic_desc = (String) picRow.get(k++);
                        String pic_path = (String) picRow.get(k++);

                        // 图片DTO
                        Pic p = new Pic();
                        p.setColor_id(color_id);
                        p.setPic_id(pic_id);
                        p.setPic_desc(pic_desc);
                        p.setPic_path(pic_path);

                        // 把图片DTO加入到对应的颜色DTO中
                        c.getPicsOfColor().add(p);
                    }

                    // 将颜色DTO加入集合中
                    colorsForRet.add(c);
                }

                return colorsForRet;
            }
            //查询设备相关评论
            case ACTION_APPEND3: {
                ArrayList<Comment> commentList = new ArrayList<Comment>();
                String device_id = (String) newData;

                // 查询对应设备的颜色
                String sql = "select eva_id,for_user_uid,so_detail_id,eva_content,eva_time,eva_score from device_evaluate where device_id='" + device_id + "'";
                Vector<Vector> comments = db.queryData(sql);
                for (Vector row : comments) {
                    Comment c = new Comment();
                    int j = 0;
                    c.setEva_id((String) row.get(j++));
                    c.setFor_user_uid((String) row.get(j++));
                    c.setSo_detail_id((String) row.get(j++));
                    c.setEva_content((String) row.get(j++));
                    c.setEva_time((String) row.get(j++));
                    c.setEva_score((String) row.get(j++));
                    c.setDevice_id(device_id);

                    commentList.add(c);
                }

                return commentList;
            }
            default:
                throw EndsExceptionFactory.INVALID_ACTION_ID(action_id);
        }
    }


    public static Feature getFeature(String device_id) throws Exception {

        Feature f = null;

        // 查询对应设备的颜色
        String sql =
                "select " +
                        "device_id" +
                        ",pic_path" +
                        ",pic_desc" +
                        " from device_featured where valid=1 and device_id='" + device_id + "'";
        Vector<Vector> features = db.queryData(sql);
        if (features.size() > 0) {
            for (Vector row : features) {
                f = new Feature();
                int j = 0;
                f.setDevice_id((String) row.get(j++));
                f.setPic_path((String) row.get(j++));
                f.setPic_desc((String) row.get(j++));
            }
        }

        return f;
    }

//	private ArrayList<SODetail> getSODetails(String order_id) throws Exception{
//		ArrayList<SODetail> datas = new ArrayList<SODetail>();
//		String sql = "SELECT " +
//							"so_detail_id,order_id,device_id," +
//							"color_id,purchase_quantity,purchase_price,appender " +
//					"FROM sales_so_detail " +
//					"WHERE order_id = '" + order_id + "'";
//		Vector<Vector> vec = db.queryData(sql);
//		if(vec.size() > 0)
//		{
//			for(Vector row : vec)
//			{
//				int j = 0;
//				SODetail d = new SODetail();
//				d.setSo_detail_id((String)row.get(j++));
//				d.setOrder_id((String)row.get(j++));
//				d.setDevice_id((String)row.get(j++));
//				d.setColor_id((String)row.get(j++));
//				d.setPurchase_quantity((String)row.get(j++));
//				d.setPurchase_price((String)row.get(j++));
//				d.setAppender((String)row.get(j++));
//				datas.add(d);
//			}
//		}
//		return datas;
//	}
//
//	private SOConsigneeInfo getSOConsigneeInfo(String order_id) throws Exception{
//		String sql = "SELECT " +
//							"order_id,consignee_surname,consignee_name," +
//							"country_of_consignee,city_of_consignee,addr_of_consignee," +
//							"code_of_consignee,mail_of_consignee,phone_of_consignee,appender " +
//					"FROM sales_so_consignee_info " +
//					"WHERE order_id = '" + order_id + "'";
//		SOConsigneeInfo d = new SOConsigneeInfo();
//		Vector<Vector> vec = db.queryData(sql);
//		for(Vector row : vec)
//		{
//			int j = 0;
//			d.setOrder_id((String)row.get(j++));
//			d.setConsignee_surname((String)row.get(j++));
//			d.setConsignee_name((String)row.get(j++));
//			d.setCountry_of_consignee((String)row.get(j++));
//			d.setCity_of_consignee((String)row.get(j++));
//			d.setAddr_of_consignee((String)row.get(j++));
//			d.setCode_of_consignee((String)row.get(j++));
//			d.setMail_of_consignee((String)row.get(j++));
//			d.setPhone_of_consignee((String)row.get(j++));
//			d.setAppender((String)row.get(j++));
//		}
//		return d;
//	}
}
